package com.huan.iteye.analysis.support.impl;

import com.huan.iteye.analysis.domain.IteyeBlogView;
import com.huan.iteye.analysis.repository.IteyeBlogViewRepository;
import com.huan.iteye.analysis.support.AbstractBlogAnalysisSupport;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Component;

import java.util.*;
import java.util.stream.Collectors;

/**
 * 近N天博客访问量
 *
 * @author huan.fu
 * @date 2018/9/18 - 16:26
 */
@Component
@Order(2)
public class NearNDayViewAnalysis extends AbstractBlogAnalysisSupport {

	@Autowired
	private IteyeBlogViewRepository iteyeBlogViewRepository;

	@Override
	public String doAnalysis(List<String> workDays) {

		Map<String, List<IteyeBlogView>> workDayBlogViews = iteyeBlogViewRepository.findByWorkDateIn(workDays)
				.stream()
				.collect(Collectors.groupingBy(IteyeBlogView::getWorkDate));

		// 日期  博客  访问量
		Map<String, Map<String, Long>> everyDayEachBlogViewMap = new HashMap<String, Map<String, Long>>(workDayBlogViews.size() * 2) {
			@Override
			public Map<String, Long> get(Object key) {
				return super.computeIfAbsent((String) key, k -> new HashMap<>(16));
			}
		};

		// 日期  博客  访问量
		workDayBlogViews.forEach((key, value) -> value.forEach(blogView -> everyDayEachBlogViewMap.get(key).putIfAbsent(blogView.getTitle(), blogView.getViewCnt())));

		// 获取到最近一天的所有的博客，因为这天的博客是最全的，也是最多的
		Set<String> blogTitles = everyDayEachBlogViewMap.get(workDays.get(workDays.size() - 1)).keySet();

		return generatorHtml(workDays, blogTitles, everyDayEachBlogViewMap);
	}

	/**
	 * 生成 html 代码
	 *
	 * @param workDays
	 * @param blogTitles
	 * @param everyDayEachBlogViewMap
	 * @return
	 */
	public String generatorHtml(List<String> workDays, Set<String> blogTitles, Map<String, Map<String, Long>> everyDayEachBlogViewMap) {
		StringBuilder builder = new StringBuilder(100);
		builder.append("<table>");
		builder.append("<caption><h3>").append("近[").append(workDays.size()).append("]天各篇博客访问量").append("</h3></caption>");
		builder.append("<thead>")
				.append("<tr>")
				.append("<th>").append("</th>");
		workDays.forEach(day -> builder.append("<th>").append(day).append("</th>"));

		builder.append("</tr>")
				.append("</thead>")
				.append("<tbody>");

		for (String blogTitle : blogTitles) {
			builder.append("<tr>").append("<td>").append(blogTitle).append("</td>");
			for (int i = 0; i < workDays.size(); i++) {
				String workDay = workDays.get(i);
				// 当前访问量
				long incrViewCnt = 0L;
				// 当前分析的最后一天,那么就是0
				if (i != workDays.size() - 1) {
					incrViewCnt = Optional.ofNullable(Optional.ofNullable(everyDayEachBlogViewMap.get(workDays.get(i + 1))).orElseGet(HashMap::new).get(blogTitle)).orElse(0L) - Optional.ofNullable(Optional.ofNullable(everyDayEachBlogViewMap.get(workDay)).orElseGet(HashMap::new).get(blogTitle)).orElse(0L);
				}

				builder.append("<td>")
						.append(Optional.ofNullable(Optional.ofNullable(everyDayEachBlogViewMap.get(workDay)).orElseGet(HashMap::new).get(blogTitle)).orElse(0L))
						.append("( ")
						.append("<span style='color:#FF0000;font-weight:bold;'>")
						.append(incrViewCnt)
						.append("</span>")
						.append(" )")
						.append("</td>");
			}
			builder.append("</tr>");
		}
		builder.append("</tbody>");
		builder.append("</table>");
		return builder.toString();
	}
}
